//
//  LSHomeView.m
//  听说弱点强化训练
//
//  Created by 盛超 on 2018/9/25.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "LSHomeView.h"
#import "BKLineChartView.h"
#import "KZShadowButton.h"
#import "LSTagLine.h"

@interface LSHomeView ()<TYAttributedLabelDelegate>

@property (nonatomic,strong) UIImageView *loadingImageView;
@property (nonatomic,strong) NSMutableDictionary *oneXdictionary;
@property (nonatomic,strong) NSMutableDictionary *secondXdictionary;
@property (nonatomic,strong) NSMutableArray *XdateArr;

@property (nonatomic,strong) UILabel *line;

@end

@implementation LSHomeView

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        UILabel *line = [[UILabel alloc] init];
        self.line = line;
        line.alpha = 0;
        [self addSubview:line];
        line.backgroundColor = [UIColor lightGrayColor];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.bottom.equalTo(self).offset(-0.5);
            make.height.mas_equalTo(0.5);
        }];
    }
    return self;
}

- (void)setWeakModel:(LSweak *)weakModel {
    _weakModel = weakModel;
    [_loadingImageView stopAnimating];
    
    while ([self.subviews lastObject]!=nil) {
        [(UIView *)[self.subviews lastObject] removeFromSuperview];
    }
    [self drawLineChart];
}

- (NSString *)compareLaterTimes:(NSArray*)array {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //将日期字符串转换成Date类型
    NSDate *startDate = [dateFormatter dateFromString:array.firstObject];
    NSDate *endDate = [dateFormatter dateFromString:array.lastObject];
    //将日期转换成时间戳
    NSTimeInterval start = [startDate timeIntervalSince1970]*1;
    NSTimeInterval end = [endDate timeIntervalSince1970]*1;
    NSTimeInterval value = end - start;
    if (value>0) {
        return array.lastObject;
    }
    
    return array.firstObject;
}

- (NSString *)comparEarlyTimes:(NSArray*)array {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    //将日期字符串转换成Date类型
    NSDate *startDate = [dateFormatter dateFromString:array.firstObject];
    NSDate *endDate = [dateFormatter dateFromString:array.lastObject];
    //将日期转换成时间戳
    NSTimeInterval start = [startDate timeIntervalSince1970]*1;
    NSTimeInterval end = [endDate timeIntervalSince1970]*1;
    NSTimeInterval value = end - start;
    if (value>0) {
        return array.firstObject;
    }
    return array.lastObject;
    
    return nil;
}

- (void)drawLineChart {
    
    if(_weakModel == nil) {
        
        UILabel *textLabel  = [[UILabel alloc] init];
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.font = [UIFont systemFontOfSize:18.0f*ScalesW];
        [self addSubview:textLabel];
        [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.right.equalTo(self).offset(-10);
            make.top.equalTo(self).offset(0);
            make.height.mas_equalTo(40);
        }];
        // textLabel.text = [NSString stringWithFormat:@"%@智能分析",_weakModel.AbiName];
        if (self.index==0) { //听力能力
            textLabel.text = @"听力能力智能分析";
            
        } else if (self.index==1) {//发音//朗读
            textLabel.text = @"口语能力智能分析";
            
        } else if (self.index==2) {//词汇
            textLabel.text = @"词汇量智能分析";
        }
        
        UIImageView *noDataImageView = [[UIImageView alloc] init];
        noDataImageView.image = [UIImage imageNamed:@"LS-暂无数据图标"];
        [self addSubview:noDataImageView];
        [noDataImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(78, 83));
        }];
        
        TYAttributedLabel *label = [[TYAttributedLabel alloc] init];
        label.text = @"暂无数据,建议先进行听说备考训练";
        label.delegate = self;
        label.textAlignment = kCTTextAlignmentCenter;
        NSRange range = [label.text rangeOfString:@"听说备考训练"];
        [label addLinkWithLinkData:@"听说备考训练" linkColor: [UIColor colorWithHex:0x6ccff6] underLineStyle:kCTUnderlineStyleSingle range:range];
        
        label.font = FONT(13);
        label.textColor = RGBA(155, 155, 155, 1);
        [self addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(noDataImageView.mas_bottom).offset(12);
            make.size.mas_equalTo(CGSizeMake(kWidth, 20));
        }];
        
        UILabel *line = [[UILabel alloc] init];
        [self addSubview:line];
        line.backgroundColor = [UIColor lightGrayColor];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.bottom.equalTo(self).offset(-0.5);
            make.height.mas_equalTo(0.5);
        }];
        
    } else {
        
        UIView *backView = [[UIView alloc] init];
        [self addSubview:backView];
        [backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsZero);
        }];
        
        UILabel *textLabel  = [[UILabel alloc] init];
        textLabel.textAlignment = NSTextAlignmentCenter;
        textLabel.font = [UIFont systemFontOfSize:18.0f*ScalesW];
        [backView addSubview:textLabel];
        [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backView).offset(10);
            make.right.equalTo(backView).offset(-10);
            make.top.equalTo(backView).offset(0);
            make.height.mas_equalTo(40);
        }];
        if ([self.weakModel.AbiClass isEqualToString:@"A"]) { //听力能力
            textLabel.text = @"听力能力智能分析";
            
        } else if ([self.weakModel.AbiClass isEqualToString:@"B"]) {//发音//朗读
            textLabel.text = @"口语能力智能分析";
            
        } else if ([self.weakModel.AbiClass isEqualToString:@"C"]) {//词汇
            textLabel.text = @"词汇量智能分析";
        }
        
        WeakObj(self);
        KZShadowButton *enterBtn = [[KZShadowButton alloc] init:^(UIButton *button) {
            
            [button setTitle:@"" forState:UIControlStateNormal];
            
            button.titleLabel.font = FONT(14*ScalesW);
            if ([selfWeak.weakModel.AbiClass isEqualToString:@"A"]) { //听力能力
                
                button.backgroundColor = RGBA(11, 178, 242, 1);
                
                // [button setTitle:@"听力能力强化训练" forState:UIControlStateNormal];
                if ([selfWeak.weakModel.WeakID isEqualToString:@"A1"]) {
                    [button setTitle:@"听力能力强化训练" forState:UIControlStateNormal];
                    [button setImage:[UIImage imageNamed:@"LS-听力能力强化训练图标"] forState:UIControlStateNormal];
                } else {
                    [button setTitle:@"错题生词强化训练" forState:UIControlStateNormal];
                    [button setImage:[UIImage imageNamed:@"LS-错题生词强化训练图标"] forState:UIControlStateNormal];
                }
                
                
            } else if ([selfWeak.weakModel.AbiClass isEqualToString:@"B"]) {//发音//朗读
                button.backgroundColor = RGBA(255,0, 0, 1)
                
                //    [button setTitle:@"口语能力强化训练" forState:UIControlStateNormal];
                if ([selfWeak.weakModel.WeakID isEqualToString:@"B1"]) {
                    [button setImage:[UIImage imageNamed:@"LS-标准发音强化训练图标"] forState:UIControlStateNormal];
                    [button setTitle:@"标准发音强化训练" forState:UIControlStateNormal];
                } else if([selfWeak.weakModel.WeakID isEqualToString:@"B2"]) {
                    [button setImage:[UIImage imageNamed:@"LS-朗读能力强化训练图标"] forState:UIControlStateNormal];
                    [button setTitle:@"朗读能力强化训练" forState:UIControlStateNormal];
                } else{
                    [button setImage:[UIImage imageNamed:@"LS-错题生词强化训练图标"] forState:UIControlStateNormal];
                    [button setTitle:@"错题生词强化训练" forState:UIControlStateNormal];
                }
                
            } else if ([selfWeak.weakModel.AbiClass isEqualToString:@"C"]) {//词汇
                button.backgroundColor = RGBA(5, 174, 9, 1);
                [button setImage:[UIImage imageNamed:@"LS-听力词汇强化学习图标"] forState:UIControlStateNormal];
                [button setTitle:@"听说词汇强化训练" forState:UIControlStateNormal];
            }
            
            button.titleEdgeInsets = UIEdgeInsetsMake(0,2, 0, 0);
            
        }];
        [enterBtn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
        [backView addSubview:enterBtn];
        enterBtn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        [enterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(backView);
            make.bottom.equalTo(backView.mas_bottom).offset(-30);
            make.size.mas_equalTo(CGSizeMake(150*ScalesW, 35*ScalesW));
        }];
        
        UILabel *leftLabel = [[UILabel alloc] init];
        [backView addSubview:leftLabel];
        leftLabel.textColor = RGBA(155, 155, 155, 1);
        leftLabel.font = [UIFont systemFontOfSize:ScalesW * 10.f];
       // leftLabel.text = @"推荐训练方式:";
        leftLabel.textAlignment = NSTextAlignmentRight;
        [leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(enterBtn.mas_left).offset(-3);
            make.centerY.equalTo(enterBtn);
            make.left.equalTo(backView);
            make.height.mas_equalTo(30);
        }];
        
        UILabel *rightLabel = [[UILabel alloc] init];
        [backView addSubview:rightLabel];
        rightLabel.textColor = RGBA(155, 155, 155, 1);
        rightLabel.font = [UIFont systemFontOfSize:ScalesW * 10.f];
       // rightLabel.text = @"(点击按钮进行训练)";
        rightLabel.textAlignment = NSTextAlignmentLeft;
        [rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(enterBtn.mas_right).offset(3);
            make.centerY.equalTo(enterBtn);
            make.right.equalTo(backView);
            make.height.mas_equalTo(30);
        }];
        
        UILabel *line = [[UILabel alloc] init];
        [backView addSubview:line];
        line.backgroundColor = [UIColor lightGrayColor];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(backView);
            make.right.equalTo(backView);
            make.bottom.equalTo(backView).offset(-0.5);
            make.height.mas_equalTo(0.5);
        }];
        
        [self handelXYData];
        BKLineChartView *lineChart = [[BKLineChartView alloc] initWithFrame:CGRectMake(30*ScalesW, 10, kWidth-60*ScalesW, 180*ScalesH)];
        [backView addSubview:lineChart];
        if ([_weakModel.AbiClass isEqualToString:@"A"]) { //听力能力
            
            lineChart.lineChartBackgroundColor =  RGBA(11, 178, 242,0.8);
            LSTagLine *lineView = [[LSTagLine alloc] initwithData:_weakModel.AbiSocreList Colors:@[lineChart.lineChartBackgroundColor]];
            [self addSubview:lineView];
            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self).offset(20);
                make.bottom.equalTo(self).offset(20);
                make.top.equalTo(enterBtn.mas_top);
                make.width.mas_equalTo(60);
            }];
            
        } else if ([_weakModel.AbiClass isEqualToString:@"B"]) { //发音
            
            lineChart.lineChartBackgroundColor =  RGBA(255,0, 0, 0.8);//红色
            lineChart.lineChartBackgroundColor2 = RGBA(128,0,128,0.8);//紫
            LSTagLine *lineView = [[LSTagLine alloc] initwithData:_weakModel.AbiSocreList Colors:@[lineChart.lineChartBackgroundColor,lineChart.lineChartBackgroundColor2]];
            [self addSubview:lineView];
            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self).offset(20);
                make.bottom.equalTo(self).offset(20);
                make.top.equalTo(enterBtn.mas_top);
                make.width.mas_equalTo(60);
            }];
            
        } else if ([_weakModel.AbiClass isEqualToString:@"C"]) { //词汇
            
            lineChart.lineChartBackgroundColor = RGBA(255, 140, 0,0.8);//橙
            lineChart.lineChartBackgroundColor2 = RGBA(5, 174, 9, 0.8);//绿
            LSTagLine *lineView = [[LSTagLine alloc] initwithData:_weakModel.AbiSocreList Colors:@[lineChart.lineChartBackgroundColor,lineChart.lineChartBackgroundColor2]];
            [self addSubview:lineView];
            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self).offset(20);
                make.bottom.equalTo(self).offset(20);
                make.top.equalTo(enterBtn.mas_top);
                make.width.mas_equalTo(60);
            }];
            
        }
        lineChart.OneXDictionary = self.oneXdictionary;
        lineChart.SecondXDictionary = self.secondXdictionary;
        [self createlabelY:lineChart];
        lineChart.XdateArr = self.XdateArr;
        
        UILabel *label = [[UILabel alloc] init];
        [backView addSubview:label];
        label.font = [UIFont systemFontOfSize:9*ScalesW];
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor redColor];
        label.transform = CGAffineTransformRotate(label.transform,-M_PI/6);
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(backView).offset(-38*ScalesW);
            make.top.equalTo(backView).offset(10);
            make.size.mas_equalTo(CGSizeMake(30*ScalesW, 50*ScalesW));
        }];
        label.text = _weakModel.WeakName;
        
        UIImageView *printImageView = [[UIImageView alloc] init];
        [backView addSubview:printImageView];
        [printImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(backView).offset(-22*ScalesW);
            make.top.equalTo(backView).offset(10);
            make.size.mas_equalTo(CGSizeMake(63*ScalesW, 50*ScalesW));
        }];
        printImageView.image = [UIImage imageNamed:@"LS-能力弱"];
        if([_weakModel.WeakID isEqualToString:@""]) {
            printImageView.image = [UIImage imageNamed:@"LS-能力强"];
        }
    }
}

//处理XY轴数据
- (void)handelXYData {
    if (_weakModel.AbiSocreList.count==2) {
        LSabiSocre *first = _weakModel.AbiSocreList.firstObject;
        LSabiSocre *last = _weakModel.AbiSocreList.lastObject;
        //第一条曲线
        // [ZDTimeString componentsSeparatedByString:@","];
        NSArray *oneTimeArr = [first.ZDTimeString componentsSeparatedByString:@","];//x轴
        NSArray *oneScoreArr = [first.ZDScoreString componentsSeparatedByString:@","];//y轴
        NSMutableDictionary *oneXdictionary = [[NSMutableDictionary alloc] init];
        for (NSInteger i = 0; i<oneTimeArr.count; i++) {
            [oneXdictionary setObject:oneScoreArr[i] forKey:oneTimeArr[i]];
        }
        self.oneXdictionary = oneXdictionary;
        
        //第二条曲线
        NSArray *secondTimeArr = [last.ZDTimeString componentsSeparatedByString:@","];//x轴
        NSArray *secondScoreArr = [last.ZDScoreString componentsSeparatedByString:@","];//y轴
        NSMutableDictionary *secondXdictionary = [[NSMutableDictionary alloc] init];
        for (NSInteger i = 0; i<secondTimeArr.count; i++) {
            [secondXdictionary setObject:secondScoreArr[i] forKey:secondTimeArr[i]];
        }
        self.secondXdictionary = secondXdictionary;
        
        
        NSString *oneLastTime = oneTimeArr.lastObject;
        NSString *secondLastTime = secondTimeArr.lastObject;
        if ([oneTimeArr.lastObject isEqualToString:@""]) {
            oneLastTime = oneTimeArr[oneTimeArr.count-2];
        }
        if ([secondTimeArr.lastObject isEqualToString:@""]) {
            secondLastTime = secondTimeArr[secondTimeArr.count-2];
        }
        
        NSString *endTime = [self compareLaterTimes:@[oneLastTime,secondLastTime]];
        
        NSString *oneFirstTime = oneTimeArr.firstObject;
        NSString *secondFirstTime = secondTimeArr.firstObject;
        NSString *startTime = [self comparEarlyTimes:@[oneFirstTime,secondFirstTime]];
        NSLog(@"start=%@,end=%@",startTime,endTime);
        
        //NSInteger day = [self DvalueEndtime:endTime StartTime:startTime];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        //将日期字符串转换成Date类型
        NSDate *startDate = [dateFormatter dateFromString:startTime];
        NSDate *endDate = [dateFormatter dateFromString:endTime];
        //将日期转换成时间戳
        NSTimeInterval start = [startDate timeIntervalSince1970]*1;
        NSTimeInterval end = [endDate timeIntervalSince1970]*1;
        
        NSTimeInterval value = end - start;
        NSInteger day = (NSInteger)value / (24 * 3600);
        
        NSMutableArray *XdateArr = [NSMutableArray array];
        for (NSInteger i = 0; i<day+1; i++) {
            NSTimeInterval time = start+i*(24 * 3600);
            NSDate * myDate=[NSDate dateWithTimeIntervalSince1970:time];
            NSString *timeStr = [dateFormatter stringFromDate:myDate];
            [XdateArr addObject:timeStr];
        }
        self.XdateArr = XdateArr;
        NSLog(@"%@",XdateArr);
        
    } else if (_weakModel.AbiSocreList.count==1) {
        
        LSabiSocre *first = _weakModel.AbiSocreList.firstObject;
        NSArray *oneTimeArr = [first.ZDTimeString componentsSeparatedByString:@","];//x轴
        NSArray *oneScoreArr = [first.ZDScoreString componentsSeparatedByString:@","];//y轴
        NSMutableDictionary *oneXdictionary = [[NSMutableDictionary alloc] init];
        for (NSInteger i = 0; i<oneTimeArr.count; i++) {
            [oneXdictionary setObject:oneScoreArr[i] forKey:oneTimeArr[i]];
        }
        self.oneXdictionary = oneXdictionary;
        
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        //将日期字符串转换成Date类型
        NSDate *startDate = [dateFormatter dateFromString:oneTimeArr.firstObject];
        NSDate *endDate = [dateFormatter dateFromString:oneTimeArr.lastObject];
        if ([oneTimeArr.lastObject isEqualToString:@""]) {
            endDate = [dateFormatter dateFromString:oneTimeArr[oneTimeArr.count-2]];
        }
        //将日期转换成时间戳
        NSTimeInterval start = [startDate timeIntervalSince1970]*1;
        NSTimeInterval end = [endDate timeIntervalSince1970]*1;
        
        NSTimeInterval value = end - start;
        NSInteger day = (NSInteger)value / (24 * 3600);
        NSMutableArray *XdateArr = [NSMutableArray array];
        for (NSInteger i = 0; i<day+1; i++) {
            NSTimeInterval time = start+i*(24 * 3600);
            NSDate * myDate=[NSDate dateWithTimeIntervalSince1970:time];
            NSString *timeStr = [dateFormatter stringFromDate:myDate];
            [XdateArr addObject:timeStr];
        }
        self.XdateArr = XdateArr;
        NSLog(@"%@",XdateArr);
    }
}

- (void)createlabelY:(BKLineChartView*)lineChart {
    
    CGFloat height = lineChart.height;
    
    NSInteger bounceY = 30;
    //NSInteger bounceX = 30;
    CGFloat differenceYValue = 100;
    if(_index==2) {
        NSMutableArray *array = [NSMutableArray array];
        for (LSabiSocre *model in _weakModel.AbiSocreList ) {
            [array addObjectsFromArray:[model.ZDScoreString componentsSeparatedByString:@","]];
        }
        NSInteger max = [self getMax:array];
        NSInteger min = [self getMin:array];
        differenceYValue = max - min;
        lineChart.YMax = max;
        lineChart.YMin = min;
    }
    for (NSInteger i = 0; i<5; i++) {
        UILabel *labelY = [[UILabel alloc] initWithFrame:CGRectMake(10,10+(height - 2 * bounceY)/5 *i + bounceY-9,18*ScalesW, bounceY/2.0)];
        labelY.tag = 2000+i;
        labelY.text = [NSString stringWithFormat:@"%d",(int)(differenceYValue/5 *(5-i))];
        labelY.font = [UIFont systemFontOfSize:10*ScalesW];
        if(_index==2) {
            labelY.font = [UIFont systemFontOfSize:9*ScalesW];
            labelY.x = 3;
            labelY.width = 25*ScalesW;
        }
        labelY.textColor = [UIColor colorWithHex:0xcfcfcf];
        labelY.textAlignment = NSTextAlignmentRight;
        
        [self addSubview:labelY];
        if (i==0) {
            UILabel *labelYname = [[UILabel alloc] initWithFrame:CGRectMake(0,labelY.y-20*ScalesW,40*ScalesW,20*ScalesW)];
            labelYname.font = [UIFont systemFontOfSize:10*ScalesW];
            labelYname.textColor = [UIColor colorWithHex:0xcfcfcf];
            labelYname.textAlignment = NSTextAlignmentCenter;
            labelYname.text = @"能力值";
            if(_index==2) {
                labelYname.text = @"词汇量";
            }
            [self addSubview:labelYname];
            
            CGFloat value = 30;
//            if ([UIScreen mainScreen].bounds.size.width > 414.0) {
//                value = 15;
//            }
            UILabel *labelXname = [[UILabel alloc] initWithFrame:CGRectMake(kWidth-30*ScalesW,lineChart.y+lineChart.height-value, 30*ScalesW, 15*ScalesW)];
            labelXname.font = [UIFont systemFontOfSize:10*ScalesW];
            labelXname.textColor = [UIColor colorWithHex:0xcfcfcf];
            labelXname.textAlignment = NSTextAlignmentCenter;
            labelXname.text = @"日期";
            [self addSubview:labelXname];
            
            //Y轴
            UILabel *labelYlline = [[UILabel alloc] initWithFrame:CGRectMake(lineChart.x,labelYname.y+labelYname.height, 0.5,labelXname.y-labelYname.y-labelYname.height)];
            labelYlline.backgroundColor = [UIColor colorWithR:207.0 g:207.0 b:207.0 a:0.5];
            [self addSubview:labelYlline];
            
            //X轴
            UILabel *labelXline = [[UILabel alloc] initWithFrame:CGRectMake(lineChart.x,labelYlline.y+labelYlline.height,lineChart.width, 0.5)];
            labelXline.backgroundColor = [UIColor colorWithR:207.0 g:207.0 b:207.0 a:0.5];
            [self addSubview:labelXline];
        }
    }
}

- (NSInteger)getMax:(NSArray*)array {
    NSInteger max = 0;
    for (NSInteger i = 0; i<array.count; i++) {
        NSNumber *num = array[i];
        if (num.integerValue>max) {
            max = num.integerValue;
        }
    }
    
    max = 100+max - max%2;
    
    return max;
}

- (NSInteger)getMin:(NSArray*)array {
    NSInteger min = 0;
    for (NSInteger i = 0; i<array.count; i++) {
        NSNumber *num = array[i];
        if (num.integerValue<min) {
            min = num.integerValue;
        }
    }
    
    min = min - min%2 -100;
    if (min<0) {
        min = 0;
    }
    
    return min;
}

- (void)pushAction:(UIControl*)sender {
    KZShadowButton *btn = (KZShadowButton*)sender;
    NSLog(@"%@",btn.button.titleLabel.text);
    self.pushBlock(btn.button.titleLabel.text);
}

#define mark - TYAttributedLabelDelegate
- (void)attributedLabel:(TYAttributedLabel *)attributedLabel textStorageClicked:(id<TYTextStorageProtocol>)textStorage atPoint:(CGPoint)point {
    NSString *string = ((TYTextStorage*)textStorage).text;
    NSLog(@"%@",string);
}

@end
